﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.LinkManage
{
	public partial class AddLink : BasePage
	{
		#region property

		private int? _classID;
		/// <summary>
		/// 当前的站点标识。
		/// </summary>
		protected int ClassID
		{
			get
			{
				if (_classID == null)
					_classID = Convert.ToInt32(Request.QueryString["ClassID"]);
				return _classID.Value;
			}
		}
		#endregion

		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
					Page.DataBind();
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				Link link = UCLink.Info;
				link.ClassId = ClassID;
				link = Links.Insert(link);

				InsertComplete(link);
			}
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Links.GetInsertRight(new Link { ClassId = ClassID });
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 插入操作完成，显示消息提示。
		/// </summary>
		public void InsertComplete(Link link)
		{
			Messages.Add(new PageMessage(false, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Insert, string.Format("ModifyLink.aspx?LinkID={0}&SiteId={1}",link.LinkId, SiteId), Res.OperationComplete_GoToModifyText));
		}
	}
}
